package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.VipListAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UserInfoBean;
import com.kj.xuyuan.model.VipListBean;
import com.kj.xuyuan.pay.PayListenerUtils;
import com.kj.xuyuan.pay.PayResultListener;
import com.kj.xuyuan.pay.PayUtils;
import com.kj.xuyuan.pop.SelectPayMethodWindow;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.kj.xuyuan.widget.FullyGridLayoutManager;
import com.kj.xuyuan.widget.HorizontalSpacingItemDecoration;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 会员中心
 */
public class VipCenterActivity extends BaseActivity implements PayResultListener {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_vip_time)
    TextView tvVipTime;
    @BindView(R.id.iv_left_button)
    ImageView ivLeftButton;
    @BindView(R.id.iv_right_button)
    ImageView ivRightButton;
    @BindView(R.id.tv_now_price)
    TextView tvNowPrice;
    @BindView(R.id.tv_old_price)
    TextView tvOldPrice;
    @BindView(R.id.iv_limit_time)
    ImageView ivLimitTime;
    @BindView(R.id.rv_vip)
    RecyclerView rvVip;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;

    private List<VipListBean> data;
    private VipListAdapter adapter;
    private int level;
    private String nickName;
    private int clickPosition;

    public static void actionStar(Context context,int level,String nickName){
        Intent intent = new Intent(context,VipCenterActivity.class);
        intent.putExtra("level",level);
        intent.putExtra("nickName",nickName);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_vip_center;
    }

    @Override
    protected void initView() {
        level = getIntent().getIntExtra("level",-1);
        nickName = getIntent().getStringExtra("nickName");
        tvVipTime.setText(level == 0 ? "当前未开通会员" : "当前已开通会员");
        tvName.setText(nickName);
        PayListenerUtils.getInstance(context).addListener(this);
        GlideUtils.loadCircleImage(context,UrlConstants.Base_Http+SPUtils.getStringData(context,Constants.AVATAR,""),ivHead);
        initRecyclerView();
        getData();
    }

    private void getData() {
        OkGoUtil.postRequest(getContext(), UrlConstants.VIP_LIST, null, new JsonCallback<BaseModel<List<VipListBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<VipListBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                       data.addAll(response.body().getData());
                       adapter.notifyDataSetChanged();
                    }else{
                        ToastUtil.showToast(getContext(),response.body().getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<List<VipListBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new VipListAdapter();
        rvVip.setLayoutManager(new FullyGridLayoutManager(context,3));
        rvVip.addItemDecoration(new HorizontalSpacingItemDecoration(3, DisplayUtil.dp2px(context,12),0));
        rvVip.setAdapter(adapter);
        adapter.setNewData(data);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                clickPosition = position;
                popSelectPayTypeWindow();
            }
        });
    }

    private void popSelectPayTypeWindow() {
        SelectPayMethodWindow popWindow = new SelectPayMethodWindow(context);
        popWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM,0,0);
        popWindow.setAnimationStyle(R.style.popwindow_center_anim_style);
        popWindow.setSelectPayMethodListener(new SelectPayMethodWindow.SelectPayMethodListener() {
            @Override
            public void selectPayMethod(int payType) {
                //1微信 2支付宝
                if(payType == 1){

                }else{
                    createAliOrder();
                }
            }
        });
    }

    private void createAliOrder() {
        Map map = new HashMap();
        map.put("id",data.get(clickPosition).getId()+"");
        map.put("type","2");
        OkGoUtil.postRequest(getContext(), UrlConstants.BUY_VIP, map, new JsonCallback<BaseModel<String>>() {
            @Override
            public void onSuccess(Response<BaseModel<String>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        PayUtils.getInstance(context).toAliPay(response.body().getData());
                    }else{
                        ToastUtil.showToast(getContext(),response.body().getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<String>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_head, R.id.tv_name})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                break;
            case R.id.tv_name:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PayListenerUtils.getInstance(context).removeListener(this);
    }

    @Override
    public void onPaySuccess() {
        finish();
    }

    @Override
    public void onPayError() {

    }

    @Override
    public void onPayCancel() {

    }
}